﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Threading.Tasks;

namespace Cherry.Agile.Distributed.Queue
{
    /// <summary>
    /// 消息队列
    /// </summary>
    [ServiceContract]
    public interface IQueueHandler
    {
        /// <summary>
        /// 获取当前未读消息，默认每队列1000项，超过会覆盖
        /// </summary>
        /// <param name="queueName">队列名称</param>
        /// <returns></returns>
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/get/{queueName}")]
        string Get(string queueName);

        /// <summary>
        ///  获取当前未读消息，默认每队列1000项，超过会覆盖
        /// </summary>
        /// <param name="queueName">队列名称</param>
        /// <param name="index">返回当前读取的index</param>
        /// <returns></returns>
        [OperationContract(Name = "GetIndex")]
        string Get(string queueName, out int index);

        /// <summary>
        /// 查看队列中指定位置的消息，查看不影响队列指针
        /// </summary>
        /// <param name="queueName"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/view/{queueName}?index={index}")]
        string View(string queueName, int index);

        /// <summary>
        /// 写入消息
        /// </summary>
        /// <param name="queueName">队列名称</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        [OperationContract]
        [WebGet(UriTemplate = "/set/{queueName}?data={data}",
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json)]
        int Set(string queueName, string data);

        /// <summary>
        /// 配置队列
        /// </summary>
        /// <param name="queueName"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        [OperationContract]
        [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.WrappedRequest,
            RequestFormat = WebMessageFormat.Json, UriTemplate = "/config/{queueName}?config={config}")]
        string Config(string queueName, string config);

        /// <summary>
        /// 队列状态
        /// </summary>
        /// <param name="queueName"></param>
        /// <returns></returns>
        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/status/{queueName}")]
        string Status(string queueName);
    }
}
